<?php

    $current_year = date('Y');
    $unix_timestamp = time();
    $max_year = $current_year + 2;

    echo $javascript->link('education_experiences');

?>

<script>

    var get_districts_list_url = "<?php echo $this->Html->url(array(
                                                            'controller' => 'EducationExperiences',
                                                            'action'    => 'get_districts_list'
                                                        )); ?>"
    var get_towns_list_url = "<?php echo $this->Html->url(array(
                                                            'controller' => 'EducationExperiences',
                                                            'action'    => 'get_towns_list'
                                                        )); ?>"                                                        
    
$(function() {
    
    /*
     * inizializza i bottoni utilizzati per aprire le dialog (site.js)
     */
    init_dialog_button('add_title', 'title_form');
    init_dialog_button('add_organization', 'organization_form');
    init_dialog_button('add_position', 'position_form');
    
    title_form_dialog("<?php echo $this->Html->url(array(
                                                        'controller' => 'EducationExperiences',
                                                        'action'    => 'add_title'
                                                    )); ?>");
                                                            
    organization_form_dialog("<?php echo $this->Html->url(array(
                                                            'controller' => 'EducationExperiences',
                                                            'action'    => 'add_organization'
                                                        )); ?>"); 
                                                                
    position_form_dialog("<?php echo $this->Html->url(array(
                                                            'controller' => 'EducationExperiences',
                                                            'action'    => 'add_position'
                                                        )); ?>");
                                                                
    // ogni volta che scelgo una regione aggiorno la select che contiene le province
    $( '#organization_region_id' ).change(function() {
            
        var region_id = $(this).val();
        if(region_id != '') {
            remove_all_options('organization_district_id', 'Selezionare la provincia');
            remove_all_options('organization_town_id', 'Selezionare la città');
            get_districts_list(region_id, get_districts_list_url, 'organization_district_id');
        }


    })
        
    // ogni volta che scelgo una provincia aggiorno la select che contiene le città
    $( '#organization_district_id' ).change(function() {

        var district_id = $(this).val();
        if(district_id != '') {
            remove_all_options('organization_city_id', 'Selezionare la città');
            get_towns_list(district_id, get_towns_list_url, 'organization_city_id');
        }

    })
 
});

</script>
<!-- Add title form dialog -->
<div id="title_form" title="Aggiungi un titolo di studio">
    <p class="validateTips">Tutte i campi sono obbligatori.</p>
    
    <form>
    <fieldset>
            <label for="title_name">Nome</label>
            <input type="text" name="title_name" id="title_name" class="text ui-widget-content ui-corner-all" />
    </fieldset>
    </form>

</div>
<!-- Add organization form dialog -->
<div id="organization_form" title="Aggiungi un'organizzazione/ente">
    <p class="validateTips">Tutte i campi sono obbligatori.</p>

    <fieldset>
            <label for="organization_name">Nome</label>
            <input type="text" name="organization_name" id="organization_name" class="text ui-widget-content ui-corner-all" />
            <label for="organization_address">Indirizzo</label>
            <input type="text" name="organization_address" id="organization_address" class="text ui-widget-content ui-corner-all" />
            <label for="organization_level">Livello</label>
            <input type="text" name="organization_level" id="organization_level" class="text ui-widget-content ui-corner-all" />
            <label for="organization_region_id">Regione</label>
            <select name="organization_region_id" id="organization_region_id" class="ui-widget-content ui-corner-all">
                <option value="">Selezionare la regione</option>
                <?php 
                    foreach($regions as $id => $region) { ?>
                        <option value="<?php echo $id ?>"><?php echo $region ?></option>
                <?php
                    }
                ?>
            </select>
            <label for="organization_district_id">Provincia</label>
            <select type="text" name="organization_district_id" id="organization_district_id" class="ui-widget-content ui-corner-all" >
                <option value="">Selezionare la provincia</option>
            </select>
            <label for="organization_city_id">Città</label>
            <select type="text" name="organization_city_id" id="organization_city_id" class="ui-widget-content ui-corner-all">
                <option value="">Selezionare la città</option>
            </select>
    </fieldset>

</div>
<!-- Add position form dialog -->
<div id="position_form" title="Aggiungi Posizione">
    <p class="validateTips">Tutte i campi sono obbligatori.</p>
    
    <form>
    <fieldset>
            <label for="position_name">Nome</label>
            <input type="text" name="position_name" id="position_name" class="text ui-widget-content ui-corner-all" />
    </fieldset>
    </form>

</div>

<div id="test_dialog" title="Aggiungi un titolo di studio">
    
</div>

<?php

    echo $form->create('EducationExperience');

?>

<fieldset>
    <legend>
        Nuova esperienza di istruzione/formazione
    </legend>

<?php

    echo $form->input('from', array('type'=>'date', 
                                    'selected'=>$unix_timestamp,
                                    'minYear'=>1920, 
                                    'maxYear'=>$max_year, 
                                    'label' => 'Da'));
    echo $form->input('to', array('type'=>'date', 
                                  'selected'=>$unix_timestamp,
                                  'minYear'=>1920, 
                                  'maxYear'=>$max_year, 
                                  'label' => 'A'));
    echo $form->input('skills', array('label' => 'Abilit&agrave;',
                                      'rows' => '3'));
    echo $form->input('title_id', array(
                                 'empty' => '-- Seleziona --',
                                 'type' => 'select',
                                 'options' => $titles,
                                 'label' => 'Titolo'));
    
    echo $html->link('Aggiungi titolo', '#', array('onclick'=>'return false;', 'id'=>'add_title', 'class'=>''));

    echo $form->input('organization_id', array(
                                 'empty' => '-- Seleziona --',
                                 'type' => 'select',
                                 'options' => $organizations,
                                 'label' => 'Organizzazione/Ente'));

    echo $html->link('Aggiungi organizzazione', '#', array('onclick'=>'return false;', 'id'=>'add_organization', 'class'=>''));                             

    echo $form->input('position_id', array(
                                 'empty' => '-- Seleziona --',
                                 'type' => 'select',
                                 'options' => $positions,
                                 'label' => 'Posizione'));
    
    echo $html->link('Aggiungi posizione', '#', array('onclick'=>'return false;', 'id'=>'add_position', 'class'=>'')); 
    
    echo $form->input('person_id', array(
                             'type' => 'hidden',
                             'value' => $person_id
                    ));

?>
    
</fieldset>

<?php

echo $form->end('Salva');

?>